package com.wf.course.web.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.utils.page.PageBean;
import com.wf.course.model.user.Role;
import com.wf.course.model.user.SysRoleMenu;
import com.wf.course.model.user.SysMenu;
import com.wf.course.service.ISysRoleMenuService;
import com.wf.course.service.IRoleService;
import com.wf.course.service.ISysMenuService;
/**
 * 
 * @content 系统角色Controller
 */
@RequestMapping("sysRole")
@Controller
public class SysRoleController {

	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private ISysMenuService sysMenuService;
	
	@Autowired
	private ISysRoleMenuService sysRoleMenuService;

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param model
	 * @return
	 */
	@RequestMapping("sysRoleList")
	public String sysRoleList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "name", required = false) String name,
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		if(name!=null && name!=""){
			cons.put("name", name);
		}
		PageBean<Role> pageBean = new PageBean<Role>(pageNo, pageSize, null, cons);
		pageBean.setUrl("sysRole/sysRoleList");
		this.roleService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "sys/sysRoleList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("pageLoad/{id}")
	public String pageLoad(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			Role role = roleService.querRoleByRoleId(id);
			model.addAttribute("sysRole", role);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		
		return "sys/sysRoleEdit";
	}

	/**
	 * 新增和修改
	 * @param SysRole
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "sysRoleEdit" , produces = "text/html;charset=UTF-8")
	public String sysRoleEdit(@ModelAttribute Role sysRole) {
		
		roleService.saveOrUpdateRole(sysRole);
		
		return "redirect:/sysRole/sysRoleList";
	}
//
//	/**
//	 * 删除
//	 * @param id
//	 * @return
//	 */
//	@RequestMapping(value = "remove/{id}")
//	public String removeSysRole(@PathVariable("id") Integer id) {
//		
//		sysRoleService.deleteById(id);
//		
//		return "redirect:/sysRole/sysRoleList";
//	}
	
	/**
	 * 分配菜单
	 * @param id 角色ID
	 * @param model
	 * @return
	 */
	@RequestMapping("assignMenu/{roleId}")
	public String assignMenu(@PathVariable("roleId") Integer roleId, Model model) {
		System.out.println("打印参数： ---》 "+roleId);
		//得到所有菜单
		List<SysMenu> MenuList = sysMenuService.queryMenuList();
		//得到角色及角色对应的菜单
		Role role = roleService.querRoleByRoleId(roleId);
		List<SysRoleMenu> ramList = sysRoleMenuService.queryRoleAndMenu(roleId);
		for(SysMenu menu:MenuList){
			for (SysRoleMenu ram : ramList) {
				if(ram.getMenuId().equals(menu.getMenuId())){
					menu.setIsSelected("checked=\"on\"");
				}
			}
		}
		model.addAttribute("role", role);	//角色信息
		model.addAttribute("MenuList", MenuList);//所有菜单
		return "sys/sysRoleAndMenu";
	}
	
	/**
	 * 重新分配菜单
	 * @param roleId 角色ID
	 * @param menuIds 拥有的菜单ID
	 * @param model
	 * @return
	 */
	@RequestMapping("againAssignMenu")
	public String againAssignMenu(@RequestParam(value = "roleId", required = false) Integer roleId,
			@RequestParam(value = "menuIds", required = false) String menuIds,Model model){
		SysRoleMenu ram = new SysRoleMenu();
		System.out.println("打印参数： ---》 "+roleId+" - - - "+menuIds.toString());
		ram.setRoleId(roleId);
		if(menuIds.contains(",")){
		   String menuId[] = menuIds.split(",");
		   //删除角色对应的菜单信息
		   sysRoleMenuService.deleteRoleAndMenu(roleId);
		   for(int i=0;i<menuId.length;i++){
			   ram.setMenuId(Integer.valueOf(menuId[i]));
			   //保存权限信息到角色和菜单中间表(role_and_menu)
			   sysRoleMenuService.saveRoleAndMenu(ram);
		   }
		}
		return assignMenu(roleId, model);
	}
	
	
	

}
